]> git.r.bdr.sh - rbdr/super-polarity/blobdiff - Super Polarity/Actors/MainShip.cs
Protoshow sprint.
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
index 1f4f22ac2128d0e194ef025f370bb90b01588ca7..9f28c80c5c51298888ceb095e59641cab87b0886 100644 (file)
@@ -6,6 +6,7 @@ using System.Threading;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Audio;
 
 namespace SuperPolarity
 {
@@ -20,7 +21,15 @@ namespace SuperPolarity
         protected bool Shooting;
         protected int ShotCooldown;
 
-        public MainShip(Game newGame) : base(newGame) {}
+        protected float CurrentImmortalTime;
+        protected float MaxImmortalTime;
+        protected bool Flashing;
+
+        protected SoundEffect PolarityChange;
+        protected SoundEffect ShootSound;
+        protected SoundEffect Hit;
+
+        public MainShip(SuperPolarity newGame) : base(newGame) {}
 
         ~MainShip()
         {
@@ -31,13 +40,26 @@ namespace SuperPolarity
         {
             base.Initialize(texture, position);
 
-            MainShip.BlueColor = new Color(230, 244, 249);
-            MainShip.RedColor = new Color(255, 234, 241);
+            MainShip.BlueColor = new Color(0, 184, 229);
+            MainShip.RedColor = new Color(201, 0, 68);
+
+            PolarityChange = game.Content.Load<SoundEffect>("Sound\\polaritychange");
+            ShootSound = game.Content.Load<SoundEffect>("Sound\\bullet");
+            Hit = game.Content.Load<SoundEffect>("Sound\\hit");
 
             InitParticleEngine();
             SetPolarity(Polarity.Positive);
 
+            ActVelocity = 2.5f;
+
             ShotCooldown = 50;
+            MaxImmortalTime = 1500;
+
+            BoxDimensions.X = 2;
+            BoxDimensions.Y = 2;
+            BoxDimensions.W = 2;
+            BoxDimensions.Z = 2;
+            InitBox();
 
             BindInput();
         }
@@ -57,15 +79,28 @@ namespace SuperPolarity
 
         protected void HandleShot(float value)
         {
-            Children.Add(ActorFactory.CreateBullet(Position, Angle));
+
             Shooting = true;
             Timer t = new Timer(new TimerCallback(UnlockShot));
             t.Change(ShotCooldown, Timeout.Infinite);
+
+            if (Children.Count > 10)
+            {
+                return;
+            }
+
+            var bullet = ActorFactory.CreateBullet(Position, Angle);
+
+            Children.Add(bullet);
+            bullet.Parent = this;
+
+            ShootSound.Play();
         }
 
         protected void UnlockShot(object state)
         {
             InputController.Unlock("shoot");
+            Shooting = false;
         }
 
         protected void HandleChangePolarity(float value)
@@ -75,28 +110,30 @@ namespace SuperPolarity
 
         public void HandleHorizontalMovement(float value)
         {
-            Acceleration.X = value * AccelerationRate;
-
-            if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
+            if (value >= -0.5 && value <= 0.5)
             {
-                Acceleration.X *= 2;
+                value = 0;
             }
 
-            if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
-            {
-                Acceleration.Y *= 2;
-            }
+            Velocity.X = value * MaxVelocity;
         }
 
         public void HandleVerticalMovement(float value)
         {
-            Acceleration.Y = value * AccelerationRate;
+            if (value >= -0.5 && value <= 0.5)
+            {
+                value = 0;
+            }
+
+            Velocity.Y = value * MaxVelocity;
         }
 
         public override void SwitchPolarity()
         {
             base.SwitchPolarity();
             SwitchParticleEngine(CurrentPolarity);
+            PolarityChange.Play();
+            game.Player.ResetMultiplier();
         }
 
         public override void SetPolarity(Polarity newPolarity)
@@ -127,37 +164,68 @@ namespace SuperPolarity
             particleEngine.EmitterLocation = Position;
             particleEngine.Update();
             ConstrainToEdges();
-            Shooting = false;
+            UpdateImmortality(gameTime);
         }
 
-        public override void Move(GameTime gameTime)
+        public void UpdateImmortality(GameTime gameTime)
         {
-            base.Move(gameTime);
-
-            if (Shooting)
+            if (Immortal)
             {
-                if (Velocity.X > ActVelocity)
-                {
-                    Velocity.X = ActVelocity;
-                }
+                CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
 
-                if (Velocity.X < -ActVelocity)
+                if (Flashing)
                 {
-                    Velocity.X = -ActVelocity;
+                    Color = new Color(255, 255, 255, 128);
                 }
-
-                if (Velocity.Y > ActVelocity)
+                else
                 {
-                    Velocity.Y = ActVelocity;
+                    Color = Color.White;
                 }
 
-                if (Velocity.Y < -ActVelocity)
+                Flashing = !Flashing;
+
+                if (CurrentImmortalTime > MaxImmortalTime)
                 {
-                    Velocity.Y = -ActVelocity;
+                    Immortal = false;
+                    Color = Color.White;
                 }
             }
         }
 
+        public override void Move(GameTime gameTime)
+        {
+            var VelocityLimit = MaxVelocity;
+            var SavedVelocity = new Vector2(Velocity.X, Velocity.Y);
+
+            if (Shooting)
+            {
+                VelocityLimit = ActVelocity;
+            }
+
+            if (SavedVelocity.X > VelocityLimit)
+            {
+                SavedVelocity.X = VelocityLimit;
+            }
+
+            if (SavedVelocity.X < -VelocityLimit)
+            {
+                SavedVelocity.X = -VelocityLimit;
+            }
+
+            if (SavedVelocity.Y > VelocityLimit)
+            {
+                SavedVelocity.Y = VelocityLimit;
+            }
+
+            if (SavedVelocity.Y < -VelocityLimit)
+            {
+                SavedVelocity.Y = -VelocityLimit;
+            }
+
+            Position.X = Position.X + SavedVelocity.X;
+            Position.Y = Position.Y + SavedVelocity.Y;
+        }
+
         public override void Magnetize(Ship ship, float distance, float angle)
         {
         }
@@ -207,5 +275,40 @@ namespace SuperPolarity
             particleEngine.Draw(spriteBatch);
             base.Draw(spriteBatch);
         }
+
+        public override void Collide(Actor other, Rectangle collision)
+        {
+            if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
+                            !Immortal)
+            {
+                Die();
+            }
+        }
+
+        protected override void Die()
+        {
+            game.Player.Lives = game.Player.Lives - 1;
+            game.Player.ResetMultiplier();
+            if (game.Player.Lives < 0)
+            {
+                Dying = true;
+                game.GameOver();
+            }
+            else {
+                Hit.Play();
+                Immortal = true;
+                CurrentImmortalTime = 0;
+            }
+        }
+
+        public override void CleanUp()
+        {
+            base.CleanUp();
+            particleEngine = null;
+            InputController.Unbind("moveX", HandleHorizontalMovement);
+            InputController.Unbind("moveY", HandleVerticalMovement);
+            InputController.Unbind("changePolarity", HandleChangePolarity);
+            InputController.Unbind("shoot", HandleShot);
+        }
     }
 }